/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.nacos.config.server.jpa.service.impl;


import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.nacos.config.server.auth.PermissionInfo;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.service.PersistService;
import com.alibaba.nacos.config.server.jpa.entity.PermissionsEntity;
import com.alibaba.nacos.config.server.jpa.repository.PermissionsRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Permission CRUD service
 *
 * @author nkorange
 * @since 1.2.0
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class PermissionPersistServiceImpl extends PersistService {

    @Autowired
    private PermissionsRepository permissionsRepository;


    public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) {
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        org.springframework.data.domain.Page<PermissionsEntity> datas;
        if (StringUtils.isBlank(role)) {
            datas = permissionsRepository.findAll(pageRequest);
        }else {
            datas = permissionsRepository.findByRole(role, pageRequest);
        }

        List<PermissionInfo> list = datas.stream().map(data -> {
            PermissionInfo permissionInfo = new PermissionInfo();
            BeanUtils.copyProperties(data, permissionInfo);
            return permissionInfo;
        }).collect(Collectors.toList());
        return convertPage(datas, pageNo, list);

    }

    private <E> Page<E> convertPage(org.springframework.data.domain.Page<?> jpaPage,
                                    int pageNo, List<E> datas) {
        Page<E> page = new Page<>();
        page.setPagesAvailable(jpaPage.getTotalPages());
        page.setTotalCount((int) jpaPage.getTotalElements());
        page.setPageNumber(pageNo);
        page.setPageItems(datas);
        return page;
    }


    public void addPermission(String role, String resource, String action) {
        PermissionsEntity data = new PermissionsEntity();
        data.setResource(resource);
        data.setAction(action);
        data.setRole(role);
        permissionsRepository.save(data);
    }

    public void deletePermission(String role, String resource, String action) {
        permissionsRepository.deleteByRoleAndActionAndResource(role, action, resource);
    }

}
